package cn.bonoon.kernel.web.components;

import java.util.Date;

import cn.bonoon.kernel.IdGenerator;
import cn.bonoon.kernel.descriptors.UIPropertyAccessor;
import cn.bonoon.kernel.web.annotations.components.AsCheckbox;
import cn.bonoon.kernel.web.annotations.components.AsComboBox;
import cn.bonoon.kernel.web.annotations.components.AsComboGrid;
import cn.bonoon.kernel.web.annotations.components.AsComboTree;
import cn.bonoon.kernel.web.annotations.components.AsComponent;
import cn.bonoon.kernel.web.annotations.components.AsDateBox;
import cn.bonoon.kernel.web.annotations.components.AsDatetimeBox;
import cn.bonoon.kernel.web.annotations.components.AsEditor;
import cn.bonoon.kernel.web.annotations.components.AsFile;
import cn.bonoon.kernel.web.annotations.components.AsNumberBox;
import cn.bonoon.kernel.web.annotations.components.AsNumberSpinner;
import cn.bonoon.kernel.web.annotations.components.AsPassword;
import cn.bonoon.kernel.web.annotations.components.AsPropertyEditor;
import cn.bonoon.kernel.web.annotations.components.AsRadio;
import cn.bonoon.kernel.web.annotations.components.AsSelector;
import cn.bonoon.kernel.web.annotations.components.AsTextArea;
import cn.bonoon.kernel.web.annotations.components.AsTimeSpinner;
import cn.bonoon.kernel.web.html.UIPropertyBuilder;
import cn.bonoon.kernel.web.html.form.ComponentPropertyParser;

public abstract class AbstractComponent implements Component{
	
	public static Component get(ComponentPropertyParser property, UIPropertyAccessor pa){
		AsDateBox adb = pa.getAnnotation(AsDateBox.class);
		if(null != adb) return new DateBoxComponent(property, pa, adb);
		AsDatetimeBox adbt = pa.getAnnotation(AsDatetimeBox.class);
		if(null != adbt) return new DatetimeBoxComponent(property, pa, adbt);
		AsComboBox acb = pa.getAnnotation(AsComboBox.class);
		if(null != acb) return new ComboBoxComponent(property, pa, acb);
		AsComboTree act = pa.getAnnotation(AsComboTree.class);
		if(null != act) return new ComboTreeComponent(property, pa, act);
		AsComboGrid acg = pa.getAnnotation(AsComboGrid.class);
		if(null != acg) return new ComboGridComponent(property, pa, acg);
		AsSelector as = pa.getAnnotation(AsSelector.class);
		if(null != as) return new SelectorComponent(property, pa, as);
		AsCheckbox ac = pa.getAnnotation(AsCheckbox.class);
		if(null != ac) return new CheckboxComponent(property, pa, ac);
		AsTextArea ata = pa.getAnnotation(AsTextArea.class);
		if(null != ata) return new TextAreaComponent(property, pa, ata);
		AsFile af = pa.getAnnotation(AsFile.class);
		if(null != af) return new FileComponent(property, pa, af);
		AsNumberBox anb = pa.getAnnotation(AsNumberBox.class);
		if(null != anb) return new NumberBoxComponent(property, pa, anb);
		AsRadio ar = pa.getAnnotation(AsRadio.class);
		if(null != ar) return new RadioComponent(property, pa, ar);
		AsPassword ap = pa.getAnnotation(AsPassword.class);
		if(null != ap) return new PasswordComponent(property, pa, ap);
		AsEditor ae = pa.getAnnotation(AsEditor.class);
		if(null != ae) return new EditorComponent(property, pa, ae);
		AsNumberSpinner ans = pa.getAnnotation(AsNumberSpinner.class);
		if(null != ans) return new NumberSpinnerComponent(property, pa, ans);
		AsTimeSpinner ats = pa.getAnnotation(AsTimeSpinner.class);
		if(null != ats) return new TimeSpinnerComponent(property, pa, ats);
//		AsImage ai = pa.getAnnotation(AsImage.class);
//		if(null != ai) return new ImageComponent(property, pa, ai);
		AsComponent com = pa.getAnnotation(AsComponent.class);
		if(null != com) return new StandardComponent(property, pa, com);
		
		//这个暂时还没实现
		AsPropertyEditor ape = pa.getAnnotation(AsPropertyEditor.class);
		if(null != ape) return new PropertyEditorComponent(property, pa, ape);
		
		//根据属性的类型来做默认的选择
		if(pa.isNumber()){
			return new NumberBoxComponent(property, pa);
		}else if(pa.isBoolean()){
			return new CheckboxComponent(property, pa, "是");
		}else if(pa.is(Date.class)){
			return new DateBoxComponent(property, pa);
		}
		return new InputComponent(property, pa);
	}
	
	protected final ComponentPropertyParser componentParser;
	protected final UIPropertyAccessor accessor;
	protected final String componentId;
	protected final String componentName;

	@Override
	public void build(UIPropertyBuilder builder) throws Exception{
		parser(builder);
	}
	
	protected abstract void parser(UIPropertyBuilder builder)throws Exception;

	protected AbstractComponent(ComponentPropertyParser componentParser, UIPropertyAccessor accessor) {
		this.componentParser = componentParser;
		this.accessor = accessor;
		this.componentName = accessor.getName();
		this.componentId = IdGenerator.fixed();//accessor.getOwnerClass().getSimpleName() + '-' + componentName;
	}
	
	protected int componentWidth(){
		int w = componentParser.getEditorwidth();
		if(componentParser.isRequired()){
			w -= fontSize;
		}
		return w;
	}
	
	/**
	 * <pre>
	 * 用于设置id和name
	 * 
	 * id='componentId'
	 * name='componentName'
	 * </pre>
	 * @param com
	 */
	protected void __component(StringBuilder com){
		com.append(" id='").append(componentId);
		com.append("' name='").append(componentName).append("' ");
	}
}
